package com.aoxon.cargo.component;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.Price;
import com.aoxon.cargo.util.ToastUtil;

public class SupPriceControl {

	public boolean allOtherInput = false;

	private ImageView ivCommitButton;
	private LinearLayout llHolder;
	private Activity activity;

	public SupPriceControl(Activity activity, LinearLayout llHolder,
			ImageView ivCommitButton) {
		this.activity = activity;
		this.llHolder = llHolder;
		this.ivCommitButton = ivCommitButton;
	}

	/***
	 * 添加价格项的操作
	 */
	public void addPriceItem(String one, String two, String price) {
		final View priceItem = activity.getLayoutInflater().inflate(
				R.layout.sup_input_goods_information_price_item, null);
		final ImageView ivDeleteButton = (ImageView) priceItem
				.findViewById(R.id.ivAddNewPriceDeleteButton);
		final EditText etNumberOne = (EditText) priceItem
				.findViewById(R.id.etAddNewPriceNumberOne);
		final EditText etNumberTwo = (EditText) priceItem
				.findViewById(R.id.etAddNewPriceNumberTwo);
		final EditText etPrice = (EditText) priceItem
				.findViewById(R.id.etAddNewPriceMoney);
		final LinearLayout llMaxNumberBlue = (LinearLayout) priceItem
				.findViewById(R.id.llMaxNumberBlue);
		final RelativeLayout rlMaxNumberRed = (RelativeLayout) priceItem
				.findViewById(R.id.rlMaxNumberRed);

		etNumberOne.setText(one);
		if (!two.equals("")) {
			if (Double.valueOf(two) == -1) {
				llMaxNumberBlue.setVisibility(View.GONE);
				rlMaxNumberRed.setVisibility(View.VISIBLE);
				etNumberTwo.setText("-1");
			} else {
				etNumberTwo.setText(two);
			}
		}
		etPrice.setText(price);

		rlMaxNumberRed.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				rlMaxNumberRed.setVisibility(View.GONE);
				llMaxNumberBlue.setVisibility(View.VISIBLE);
				etNumberTwo.setText("");
			}
		});

		llMaxNumberBlue.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				llMaxNumberBlue.setVisibility(View.GONE);
				rlMaxNumberRed.setVisibility(View.VISIBLE);
				etNumberTwo.setText("-1");

			}
		});
		ivDeleteButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (1 != llHolder.getChildCount()) {
					llHolder.removeView(priceItem);
				} else {
					etNumberOne.setText("");
					etNumberTwo.setText("");
					etPrice.setText("");
				}
				if (allOtherInput && checkPriceInput(false)) {
					ivCommitButton.setEnabled(true);
				} else {
					ivCommitButton.setEnabled(false);
				}
			}
		});

		etNumberOne.addTextChangedListener(myTextWatcher);
		etNumberTwo.addTextChangedListener(myTextWatcher);
		etPrice.addTextChangedListener(myTextWatcher);
		llHolder.addView(priceItem);
	}

	/**
	 * 检测价格栏的输入情况
	 * 
	 * @return
	 */
	public boolean checkPriceInput(boolean isTip) {
		for (int i = 0; i < llHolder.getChildCount(); i++) {
			View priceItem = llHolder.getChildAt(i);
			EditText etNumberOne = (EditText) priceItem
					.findViewById(R.id.etAddNewPriceNumberOne);
			EditText etNumberTwo = (EditText) priceItem
					.findViewById(R.id.etAddNewPriceNumberTwo);
			EditText etPrice = (EditText) priceItem
					.findViewById(R.id.etAddNewPriceMoney);
			if (etNumberOne.getText().toString() == null
					|| etNumberOne.getText().toString().equals("")) {
				etNumberOne.requestFocus();
				if (isTip) {
					ToastUtil.show(activity, "数量不能为空");
				}
				return false;
			}
			if (etNumberTwo.getText().toString() == null
					|| etNumberTwo.getText().toString().equals("")) {
				etNumberTwo.requestFocus();
				if (isTip) {
					ToastUtil.show(activity, "数量不能为空");
				}
				return false;
			}
			if (etPrice.getText().toString() == null
					|| etPrice.getText().toString().equals("")) {
				etPrice.requestFocus();
				if (isTip) {
					ToastUtil.show(activity, "价格不能为空");
				}
				return false;
			}
		}
		return true;
	}

	/**
	 * 获取价格列表
	 * 
	 * @return
	 */
	public List<Price> getPriceList() {
		List<Price> priceList = new ArrayList<Price>();

		for (int i = 0; i < llHolder.getChildCount(); i++) {
			View priceItem = llHolder.getChildAt(i);
			EditText etNumberOne = (EditText) priceItem
					.findViewById(R.id.etAddNewPriceNumberOne);
			EditText etNumberTwo = (EditText) priceItem
					.findViewById(R.id.etAddNewPriceNumberTwo);
			EditText etPrice = (EditText) priceItem
					.findViewById(R.id.etAddNewPriceMoney);
			Price price = new Price();

			price.setWantNumberFrom(etNumberOne.getText().toString());
			price.setWantNumberTo(etNumberTwo.getText().toString());
			price.setPrice(Double.valueOf(etPrice.getText().toString()));
			priceList.add(price);
		}

		return priceList;
	}

	/**
	 * 输入框编辑监听
	 */
	TextWatcher myTextWatcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
		}

		@Override
		public void afterTextChanged(Editable arg0) {
			// TODO Auto-generated method stub
			if (allOtherInput && checkPriceInput(false)) {
				ivCommitButton.setEnabled(true);
			} else {
				ivCommitButton.setEnabled(false);
			}
		}
	};

}
